//
//  RegistViewController.m
//  盘盈贷1.2
//
//  Created by iOS on 15/3/6.
//  Copyright (c) 2015年 iOS. All rights reserved.
//

#import "RegistViewController.h"
#import "TelephoneViewController.h"
#import "APIRequest.h"
@interface RegistViewController ()
{
    UITextField *name;
    UITextField *password;
    UITextField *password1;
    UITextView *textView;
}
@end

@implementation RegistViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.title = @"用户注册";
        self.view.backgroundColor = [UIColor whiteColor];
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    CGFloat width =   self.view.frame.size.width;
    CGFloat height = self.view.frame.size.height;
    
    name = [[UITextField alloc] initWithFrame:CGRectMake(50, 80, width / 1.4, 40)];
    name.placeholder = @"请输入用户名";
    name.borderStyle = UITextBorderStyleRoundedRect;
    name.clearButtonMode = UITextFieldViewModeWhileEditing;
    name.textColor = [UIColor grayColor];
    name.borderStyle = UITextBorderStyleLine;
    [self.view addSubview:name];
    [name release];
    
    
    password = [[UITextField alloc] initWithFrame:CGRectMake(50, 130, width / 1.4, 40)];
    password.placeholder = @"请设置登陆密码";
    password.borderStyle = UITextBorderStyleRoundedRect;
    password.clearButtonMode = UITextFieldViewModeWhileEditing;
    password.textColor = [UIColor grayColor];
    password.secureTextEntry = YES; //密码
    password.borderStyle = UITextBorderStyleLine;
    [self.view addSubview:password];
    [password release];
    
    password1 = [[UITextField alloc] initWithFrame:CGRectMake(50, 180, width / 1.4, 40)];
    password1.borderStyle = UITextBorderStyleRoundedRect;
    password1.clearButtonMode = UITextFieldViewModeWhileEditing;
    password1.placeholder = @"请再次输入密码";
    password1.textColor = [UIColor grayColor];
    password1.secureTextEntry = YES; //密码
    password1.borderStyle = UITextBorderStyleLine;
    [self.view addSubview:password1];
    [password1 release];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(10, 260, width / 1.4, 40);
    button.center = CGPointMake(width / 2, 260);
    button.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
    [button setTitle:@"下一步" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor redColor];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(next:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 300, width, 300)];
    textView.textColor = [UIColor cyanColor];
    textView.backgroundColor = [UIColor whiteColor];
    textView.userInteractionEnabled = NO;
    [self.view addSubview:textView];
    [textView release];

}

- (void)next:(id)sender
{
    if(password.text.length == 0){
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请填写密码" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        return;
    }
//    if (password.text != password1.text) {
//        UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"密码输入不一致" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
//        [alert1 show];
//        return;
//    }
    APIRequest *manage = [APIRequest sharedClient];
    manage.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:name.text, @"username",  nil];
  
    [manage POST:@"account/IsExsistUserName?" parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if ([[[responseObject firstObject] objectForKey:@"value"]isEqualToString:@"true"]) {

            TelephoneViewController *tele = [[TelephoneViewController alloc] init];
            [self.navigationController pushViewController:tele animated:YES];

            tele.name = name.text;
            tele.pwd = password.text;
           
        } else {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"用户名已存在" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        textView.text = [NSString stringWithFormat:@"error:%@",error];
        NSLog(@"error ===== %@",textView.text);
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
    }];
    
   
    
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [name resignFirstResponder];
    [password resignFirstResponder];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
